<? 
	include('base.php');
	User::protect();
	include_class('links');

	$section = 'links';
	$editors = array('description');
	$page_title = 'Add Link';

	$category_id = ($_GET['category_id'] > 0) ? $_GET['category_id'] : $_POST['category_id'];
	$id = ($_GET['id'] > 0 && is_numeric($_GET['id'])) ? $_GET['id'] : 0;

	$lc = LinkCategory::get($category_id);

	if ($_POST['submit']) { 
		$res = Link::add($_POST);
		if (!db::isError($res)) { 
			header('Location: link_edit.php?category_id=' . $category_id . '&id=' . $res->getID());
		}
	}	

	include('layout/header.php');
?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="links.php">Links &#62;</a>

	<?
	$breadcrumb = $lc->getCategoryTrail();

	if ($breadcrumb) { 
		foreach($breadcrumb as $lcBC) {
			echo('<a href="links.php?category_id=' . $lcBC->getID() . '">' . $lcBC->getName() . ' &#62;</a> ');
		}
	}
	if ($lc->error) { ?>
		<br><span class="error">
			<?= $lc->error ?>
		</span> <?
	}
	?>

	Add Link
</div>
<?

if (User::isAdmin()) { 
	if (db::isError($res)) {
		$res->outputList();
	}
	
?>

	<h1>new entry:</h1>
	<div class="inset">
	<form id="add_new_entry" action="<?=$PHP_SELF?>" method="post">
	<input type="hidden" name="category_id" value="<?= $category_id ?>">
	<div class="edit-form">
		<strong>Name</strong><br/>
		<input type="text" name="name" value="<?= $_POST['name'] ?>" class="text">
	</div>
	<div class="edit-form">
		<strong>URL</strong><br/>
		<? $url = ($_POST) ? $_POST['url'] : "http://"; ?>
		<input type="text" name="url" value="<?= $url ?>" class="text">
	</div>
	<div class="edit-form">
		<strong>Description</strong><br/>
		<textarea name="description" class="full" id="description"><?= $_POST['description'] ?></textarea>
	</div>
	<div class="edit-form">
		<input type="submit" value="Add Link" class="button" name="submit">
		<input type="submit" value="Exit Without Adding" class="button" name="preview" onclick="location.href='links.php?category_id=<?= $category_id ?>'; return false">
	</div>
	</div>
	
<? } else {

	Error::outputDialog('Return to Links', 'links.php', 'Only an administrator may add links.');

}

include('layout/footer.php'); ?>
